package com.zyd.blog.homeart.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.entity.Role;
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.homeart.service.BackstageSoftProductService;
import com.zyd.blog.persistence.beans.*;
import com.zyd.blog.persistence.beans.management.Shop;
import com.zyd.blog.persistence.mapper.*;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liusongqi
 * @since 2020-03-17
 */
@Service
public class BackstageSoftProductServiceImpl implements BackstageSoftProductService {
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private MyParticipateMapper myParticipateMapper;
    @Autowired
    private BackstageSoftProductMapper backstageSoftProductMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CenterMapper centerMapper;
    @Autowired
    private CommentRelationMapper commentRelationMapper;
    @Autowired
    private ReportTypeMapper reportTypeMapper;
    @Autowired
    private DictionariesMapper dictionariesMapper;
    @Autowired
    private SoftProductMapper softProductMapper;
    @Override
    public PageResult softBackstage(int page, int count) {
        PageResult pageResult = new PageResult();
//        Map<String,Object> map = new HashMap<>();
//        map.put("page",page);
        int i = backstageSoftProductMapper.selectCount(null);
//        if (i%count==0){
//            map.put("allPage",i/count);
//        }else {
//            map.put("allPage",i/count+1);
//        }
//        map.put("list",backstageSoftProductMapper.softBackstage(page * count - count, count));
        pageResult.setTotal((long)i);
        List<SoftProduct> softProducts = backstageSoftProductMapper.softBackstage(page * count - count, count);
        for (int j = 0; j < softProducts.size(); j++) {
            if (softProducts.get(j).getSoftStyle().equals("12")){
                softProducts.get(j).setSoftStyle(softProducts.get(j).getStyleContent());
            }else if (softProducts.get(j).getSoftPlace().equals("12")){
                softProducts.get(j).setSoftPlace(softProducts.get(j).getPlaceContent());
            }
        }
        pageResult.setRows(softProducts);
        return pageResult;
    }

    @Override
    public Integer insertSoft(SoftProduct softProduct, String list) {
        List<Shop> shops = JSONArray.parseArray(list, Shop.class);
        //用作soft表的  imageId
        List<String> uuidList = new ArrayList<>();
        //用作soft表的  image  图片路径
        List<String> imageUrl = new ArrayList<>();
        for (Shop shop : shops) {
            ReportType reportType = new ReportType();
            String uuid = UuidUtils.getUUID();
            uuidList.add(uuid);
            reportType.setId(uuid);
            reportType.setState("1");
            reportType.setContent(shop.getImageContent());
            reportTypeMapper.insert(reportType);
            imageUrl.add(shop.getImage());
            String[] split = shop.getShopId().split(",");
            for (int j = 0; j < split.length; j++) {
                Product product = new Product();
                product.setProductId(UuidUtils.getUUID());
                product.setImageId(uuid);
                product.setShopLink("商品的Id：" + j);
                product.setShopPrice(2.22 + j);
                product.setShopContent("商品的描述：" + j);
                product.setShopImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3663179458,4263162468&fm=15&gp=0.jpg");
                productMapper.insert(product);
            }
        }
        softProduct.setSoftId(UuidUtils.getUUID());
        softProduct.setSoftImage(StringUtils.join(imageUrl, ",,"));
        softProduct.setImageId(StringUtils.join(uuidList, ",,"));
        softProduct.setZanNumber(0);
        softProduct.setCollectionNumber(0);
        softProduct.setDiscussNumber(0);
        softProduct.setNow("1");
        softProduct.setAddTime(sdf.format(System.currentTimeMillis()));
        softProduct.setTopTime(sdf.format(System.currentTimeMillis()));
        softProduct.setNum(0);
        softProduct.setApartmentContent("6");
        backstageSoftProductMapper.insert(softProduct);
        return null;
    }

    @Override
    public Integer editSoft(SoftProduct softProduct, String list, String id) {
        String[] split1 = id.split(",,");
        for (String s : split1) {
            productMapper.deletePr(s);
            reportTypeMapper.deleteRe(s);
        }
        List<Shop> shops = JSONArray.parseArray(list, Shop.class);
        //用作soft表的  imageId
        List<String> uuidList = new ArrayList<>();
        //用作soft表的  image  图片路径
        List<String> imageUrl = new ArrayList<>();
        for (Shop shop : shops) {
            ReportType reportType = new ReportType();
            String uuid = UuidUtils.getUUID();
            uuidList.add(uuid);
            reportType.setId(uuid);
            reportType.setState("1");
            reportType.setContent(shop.getImageContent());
            reportTypeMapper.insert(reportType);
            imageUrl.add(shop.getImage());
            String[] split = shop.getShopId().split(",");
            for (int j = 0; j < split.length; j++) {
                Product product = new Product();
                product.setProductId(UuidUtils.getUUID());
                product.setImageId(uuid);
                product.setShopLink("商品的Id：" + j);
                product.setShopPrice(2.22 + j);
                product.setShopContent("商品的描述：" + j);
                product.setShopImage("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3663179458,4263162468&fm=15&gp=0.jpg");
                productMapper.insert(product);
            }
        }
        backstageSoftProductMapper.editSoftHave(softProduct.getSoftId(), softProduct.getSoftTitle(), softProduct.getSoftStyle(), softProduct.getSoftApartment(), softProduct.getSoftPlace(),
                softProduct.getTop(), softProduct.getUseState(), softProduct.getCoverImage(), softProduct.getProvince(),
                softProduct.getCity(), softProduct.getArea(), softProduct.getStyleContent(), softProduct.getApartmentContent(),
                softProduct.getPlaceContent(), StringUtils.join(imageUrl, ",,"), StringUtils.join(uuidList, ",,"));

        return 1;
    }

    @Override
    public Integer deleteSoft(String softId) throws Exception {
        SoftProduct softProduct = backstageSoftProductMapper.imageId(softId);
        String[] split1 = softProduct.getImageId().split(",,");
        for (String s : split1) {
            productMapper.deletePr(s);
            reportTypeMapper.deleteRe(s);
        }
        //根据文章id找到评论id  放到list
        List<CommentId> commentIds = commentMapper.commentAll(softId);
        for (CommentId commentId : commentIds) {
            try {
                commentMapper.deleteComment(commentId.getCommentId());
            } catch (Exception e) {
                throw new Exception("删除评论失败");
            }
            try {
                commentRelationMapper.deleteCommentRelation(commentId.getCommentId());
            } catch (Exception e) {
                throw new Exception("删除comment_relation失败");
            }
            try {
                commentMapper.deleteCommentReply(commentId.getCommentId());
            } catch (Exception e) {
                throw new Exception("删除评论回复失败");
            }
        }
        try {
            centerMapper.deleteCenter(softId);
        } catch (Exception e) {
            throw new Exception("删除center失败！");
        }
        try {
            myParticipateMapper.deleteArticle(softId);
        } catch (Exception e) {
            throw new Exception("从我的参与中删除失败");
        }
        try {
            backstageSoftProductMapper.deleteSoft(softId);

        } catch (Exception e) {
            throw new Exception("删除文章失败");
        }
        return 1;
    }

    @Override
    public Map<String,Object>  searchDesign(String value, int page, int count) {
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        int i = backstageSoftProductMapper.count(value);
        if (i%count==0){
            map.put("allPage",i/count);
        }else {
            map.put("allPage",i/count+1);
        }
        map.put("list",backstageSoftProductMapper.searchDesign(value,page*count-count,count));
        return map;
    }

    @Override
    public Map<String, Object> specific(String designId) throws Exception {
        Map<String,Object> map = new HashMap<>();
        try {
            SoftProduct specific = backstageSoftProductMapper.get(designId);
            specific.setSoftStyle(dictionariesMapper.content(specific.getSoftStyle()));
            specific.setSoftApartment(dictionariesMapper.content(specific.getSoftApartment()));
            specific.setSoftPlace(dictionariesMapper.content(specific.getSoftPlace()));
            String[] split = specific.getImageId().split(",,");
            String[] iamgeSplit = specific.getSoftImage().split(",,");
            List<Map> list = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                Map<String,Object> haMap = new HashMap<>();
                List<ProductNew> product = backstageSoftProductMapper.product(split[i]);
                haMap.put("count",product.size());
                haMap.put("image",iamgeSplit[i]);
                haMap.put("product",product);
                haMap.put("content",product.get(0).getContent());
                list.add(haMap);
            }
            map.put("specific",specific);
            map.put("list",list);

        } catch (Exception e) {
            throw new Exception("获取内容失败");
        }
        return map;
    }

    @Override
    public SoftProduct get(String id) {
        return backstageSoftProductMapper.get(id);
    }

    @Override
    public Integer hide(String id, String commentId, String content) {
        //此篇文章  评论数量-1
        Integer integer=0;
        Integer hide=0;
        if (content.equals("隐藏")){
            softProductMapper.commentReduce(id);

            hide = commentMapper.hide(commentId);
        }
        if (content.equals("不隐藏")){
            softProductMapper.commentNum(id);
            integer = commentMapper.noHide(commentId);
        }
        return hide+integer;
    }
}
